﻿--[[
   @brief    
   @author   rickonzhang
   @version  1.0
   @date     2015-03-12
--]]

--[[
游戏开始后隐藏
拉起失败后隐藏
]]
lga_control = {
    ---------------------------
    is_timers_start = false;
    timer_tick = 0;
    zHealthCfg = {};
    bHealthShow = false;        -- 健康公告TIPS是否显示

    ---------------------------
    init = function ( )
        base_utility.log("lga_control.init called");
        ierd_tgp.tpf_ui.Require("launch_game_animation\\view.lua");
        ierd_tgp.tpf_ui.Require("launch_game_animation\\model.lua");

        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= tpfui then
            tpfui:AddLuaListener(ierd_tgp.game_library.K_LANUCH_FIX_PROGRESS, "lga_control.on_game_repair_process");
            tpfui:AddLuaListener(ierd_tgp.game_library.K_LANUCH_FIX_RESULT, "lga_control.on_game_repair_result");
            tpfui:AddLuaListener(ierd_tgp.game_library.K_LANUCH_FIX_END, "lga_control.on_game_repair_complete");
            tpfui:AddLuaListener(ierd_tgp.game_library.K_HIDDEN_LAUNCH_GAME_UI, "lga_control.on_hide_launch_games");
            tpfui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_LAUNCH_FAIL, "lga_control.on_hide_launch_games");
            --tpfui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_ANTI_PIRACY_SUCCESS, "lga_control.on_show_launch_anti_game_success");
            tpfui:AddLuaListener(ierd_tgp.game_library.SYS_TIP_GAME_INIT_RESULT, "lga_control.on_show_game_sdk_init_success");
            -- 启动营销浮层
            tpfui:AddLuaListener(ierd_tgp.tips_mgr.ON_SHOW_LAUNCH_TIPS, "lga_control.on_show_launch_tips");
        end;

        -- 请求配置
        lga_control.QueryHealthCfg();
    end;

    uninit = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= tpfui then
            tpfui:RemoveLuaListener(ierd_tgp.game_library.K_LANUCH_FIX_PROGRESS, "lga_control.on_game_repair_process");
            tpfui:RemoveLuaListener(ierd_tgp.game_library.K_LANUCH_FIX_RESULT, "lga_control.on_game_repair_result");
            tpfui:RemoveLuaListener(ierd_tgp.game_library.K_LANUCH_FIX_END, "lga_control.on_game_repair_complete");
            tpfui:RemoveLuaListener(ierd_tgp.game_library.K_HIDDEN_LAUNCH_GAME_UI, "lga_control.on_hide_launch_games");
            tpfui:RemoveLuaListener(ierd_tgp.game_library.SYS_TIP_LAUNCH_FAIL, "lga_control.on_hide_launch_games");
            tpfui:RemoveLuaListener(ierd_tgp.game_library.SYS_TIP_GAME_INIT_RESULT, "lga_control.on_show_game_sdk_init_success");
            -- 启动营销浮层
            tpfui:RemoveLuaListener(ierd_tgp.tips_mgr.ON_SHOW_LAUNCH_TIPS, "lga_control.on_show_launch_tips");
        end;
    end;

    -- 查询健康公告提示
    QueryHealthCfg = function()
        TOssConfig.QueryCfg("health_tips_cfg.js", "lga_control.OnGetOssCfg", true);
    end;

    -- 获取配置结果
    OnGetOssCfg = function(sFile, zJson)
        if nil == zJson then
            TGPLog.LogI("HealthCfg", "Get config fail.");
            return;
        end;

        local   zData = zJson["data"];

        if (nil == zData) then
            TGPLog.LogE("HealthCfg", "Data field not find.");
            return;
        end;

        lga_control.zHealthCfg = zData;
    end;

    NeedShowHealthTips = function(nGameID)
        local bShowHealthTips = false;
        if nil == lga_control.zHealthCfg or #lga_control.zHealthCfg <= 0 then
            -- 未拉取到配置时，发行游戏默认显示健康忠告
            if lga_control.IsRailGame(nGameID) == true then
                bShowHealthTips = true;
            else
                bShowHealthTips = false;
            end;
            return bShowHealthTips;
        end;
        local isInOss = false;

        for nIdx = 1, #lga_control.zHealthCfg do
            local   zTemp = lga_control.zHealthCfg[nIdx];

            if (tonumber(zTemp["game_id"]) == nGameID) then
                isInOss = true;
                if tonumber(zTemp["show"]) ~= 0 then
                    bShowHealthTips = true;
                else
                    bShowHealthTips = false;
                end;
                break;
            end;
        end;
        if isInOss == true then
            return bShowHealthTips;
        else
            -- 添加逻辑：未在oss上配置的单机游戏，默认显示公告；未在oss上配置的端游，默认不显示公告
            if lga_control.IsRailGame(nGameID) == true then
                bShowHealthTips = true;
            else
                bShowHealthTips = false;
            end;
            return bShowHealthTips;
        end;

        return false;
    end;

    IsRailGame = function(nGameID)
        local tSvrDataIn = {game_id = nGameID};
        local tSvrDataOut = comm_center.call_service("Svr_GetIsRailGame", tSvrDataIn);
        if tSvrDataOut.is_rail_game then
            return true;
        else
            return false;
        end;
    end;

    --无检测启动
    on_show_launch_games = function(event_id, json_str)
        base_utility.log("[lga_control] on_show_launch_games...");
        local param_tab = base_utility.json_to_table(json_str);
        lga_model.game_id = param_tab["game_id"];
        lga_model.server_id = param_tab["server_id"];
        local is_cloud_game = param_tab["is_cloud_game"];
        if is_cloud_game == false then
            is_cloud_game = nil;
        end
        local base_info = banner_model.get_game_base_info(lga_model.game_id);
        local is_rail_game = false;
        if base_info ~= nil then
            if base_info.distribution_channels == banner_model.distribution_channels.CHANNELS_RAIL then
                is_rail_game = true;
            end
        end
        
        if lga_model.game_id == lga_model.current_large_tips_game_id then
            lga_view.show(false);
        else
            lga_control.bHealthShow = lga_control.NeedShowHealthTips(lga_model.game_id);

            if is_rail_game then
                lga_view.show(true, false, true, lga_control.bHealthShow, is_cloud_game);
            else
                lga_view.show(true, false, false, lga_control.bHealthShow, is_cloud_game);
            end
        end
    end;

    start_timer = function ()
        if lga_control.is_timers_start == false then
            lga_control.is_timers_start = true;
            lga_control.timer_tick = 0;

            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddInterval("lga_control.process_100ms_tick", 1000);
        end
    end;

    stop_timer = function ()
        if lga_control.is_timers_start == true then
            lga_control.is_timers_start = false;

            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:RemoveInterval("lga_control.process_100ms_tick");
        end
    end;

    process_100ms_tick = function ()
        lga_view.time_100ms_tick();
    end;
    
    on_show_launch_anti_game_success = function(event_id, json_str)
        base_utility.log("[lga_control] on_show_launch_anti_game_success...");

        if lga_control.bHealthShow then
            lga_view.delay_hide();
        else
            lga_view.show(false);
        end;
    end;

    on_show_game_sdk_init_success = function(event_id, json_str)
        base_utility.log("[lga_control] on_show_game_sdk_init_success...");

       if lga_control.bHealthShow then
            lga_view.delay_hide();
        else
            lga_view.show(false);
        end;
    end;


    on_hide_launch_games = function(event_id, json_str)
        base_utility.log("[lga_control] on_hide_launch_games...");
        --local param_tab = base_utility.json_to_table(json_str);
        --local game_id = param_tab["game_id"];
        -- 检测一下时间，如果时间不够，不关闭

        if lga_control.bHealthShow then
            local   zParam = base_utility.json_to_table(json_str);

            if (nil ~= zParam["forced"]) and (0 ~= zParam["forced"]) then
                lga_view.hide();
            else
                lga_view.delay_hide();
            end;
        else
            lga_view.hide();
        end;
        -- lga_view.show(false);
    end;

    --检测修复启动
    on_show_repair_games = function(event_id, json_str)
        base_utility.log("[lga_control] on_show_repair_games...");
        local param_tab = base_utility.json_to_table(json_str);
        lga_model.game_id = param_tab["game_id"];
        lga_model.server_id = 0;

        lga_view.show(true, true, false, lga_control.NeedShowHealthTips(lga_model.game_id));
    end;

    -- 检查或修复进度
    on_game_repair_process = function(event_id, json_str)
        -- 解析出参数，然后调用view的方法
        jtable = base_utility.json_to_table(json_str);
        lga_view.on_game_repair_process(jtable["game_id"],jtable["is_check_step"],jtable["progress"],jtable["status"]);
    end;

    -- 检查或修复结果
    on_game_repair_result = function(event_id, json_str)
        -- 解析出参数，然后调用view的方法
        base_utility.log("table param is result"..json_str);
        jtable = base_utility.json_to_table(json_str);
        lga_view.on_game_repair_result(jtable["game_id"],jtable["is_check_step"],jtable["result"]);
    end;

    -- 修复结束
    on_game_repair_complete = function(event_id, json_str)
        --lga_view.show(false);
    end;

    -- 跳过当前步骤，不再修复
    skip_this_step = function()
        base_utility.log("[lga_control]:skip this step");
        --调用C++逻辑
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        gamelib:cancel_launch_fix(lga_model.game_id);
    end;

    set_progress = function(value)
        lga_view.set_progress(value);
    end;

    ----------------------
    get_game_info = function()
        local game_icon_url = "";
        local game_net_ping = 999;

        local game_base_info = banner_model.get_game_base_info(lga_model.game_id);
        if game_base_info then            
            game_icon_url = game_base_info.banner_icon_url;
        end

        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local game_servers = ierd_tgp.game_library.GameServers:new();
        gamelib:get_game_servers_info(lga_model.game_id, game_servers);

        local loop_cnt = game_servers.server_count - 1;
        for i = 0,  loop_cnt do
            local one_server_info = game_servers.servers[i];
            base_utility.log("one_server_info.id:"..one_server_info.id..", direct_delay:"..one_server_info.acce_info.direct_delay);
            if one_server_info.id == lga_model.server_id then
                game_net_ping = one_server_info.acce_info.direct_delay; --直连的速度
                break;
            end
        end
        game_servers:delete();
        return game_icon_url, game_net_ping;

    end;

    set_current_dialog_id = function(id)
        lga_model.current_dialog_id = id;
        base_utility.log("[lga_control]:set_current_dialog_id: id is "..tostring(id));
    end;

    -- 显示tips
    on_show_launch_tips = function(event_id, json_str)
        jtable = base_utility.json_to_table(json_str);
        lga_model.current_large_tips_game_id = jtable["game_id"];
        if lga_model.game_id == lga_model.current_large_tips_game_id then
            lga_view.show(false);
        end
    end;

    -- 不用做事情，仅仅为了把模块加载起来
    OnMainWndShow = function(zParam)
    end;

};

lga_control.init();